package com.wuliu.action.line;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.json.JSONObject;

import com.wuliu.action.BaseAction;
import com.wuliu.business.line.LineObj;
import com.wuliu.model.common.PageVO;
import com.wuliu.model.common.QueryVO;
import com.wuliu.model.line.Line;
import com.wuliu.model.sys.User;
import com.wuliu.util.BaseUtil;
import com.wuliu.util.DbUtil;

public class LineAction extends BaseAction {
	private Line line;
	
    public Line getLine() {
		return line;
	}

	public void setLine(Line line) {
		this.line = line;
	}

	public String lineList() {  
		List<QueryVO> list = new ArrayList<QueryVO>(); 
        QueryVO LINE_ID = new QueryVO("A.LINE_ID", "选择", "checkbox", "chioce", "");
        list.add(LINE_ID); 
        
        QueryVO  LINE_CODE = new QueryVO("A.LINE_CODE", "配送点编码", "1");
        list.add(LINE_CODE);
        
        QueryVO  LINE_NAME = new QueryVO("A.LINE_NAME", "配送点名称", "1");
        list.add(LINE_NAME);
        
        QueryVO  PROVINCE = new QueryVO("A.PROVINCE", "省", "1");
        list.add(PROVINCE);
        
        QueryVO  CITY = new QueryVO("A.CITY", "市", "1");
        list.add(CITY);
        
        QueryVO  TOWN = new QueryVO("A.TOWN", "县", "1");
        list.add(TOWN);
        
        QueryVO  LINE_ADDR = new QueryVO("A.LINE_ADDR", "详细地址", "1");
        list.add(LINE_ADDR);
        
        QueryVO LINE_TEL = new QueryVO("A.LINE_TEL", "配送点电话", "1");
        list.add(LINE_TEL);
        
        QueryVO ACTIVE_FLAG = new QueryVO("(CASE WHEN A.ACTIVE_FLAG='0' THEN '可用' ELSE '不可用' END) AS ACTIVE_FLAG ", "是否可用", "1");
        list.add(ACTIVE_FLAG);
        
        QueryVO ADD_BY = new QueryVO("A.ADD_BY", "创建人", "1");
        list.add(ADD_BY);  
        
        QueryVO ADD_DATE = new QueryVO("A.ADD_DATE", "创建日期", "1");
        list.add(ADD_DATE);
        
        QueryVO ADD_TIME = new QueryVO("A.ADD_TIME", "创建时间", "1");
        list.add(ADD_TIME);
        
        QueryVO UPD_BY = new QueryVO("A.UPD_BY", "最后修改人", "1");
        list.add(UPD_BY);  
        
        QueryVO UPD_DATE = new QueryVO("A.UPD_DATE", "最后修改日期", "1");
        list.add(UPD_DATE);
        
        QueryVO UPD_TIME = new QueryVO("A.UPD_TIME", "最后修改时间", "1");
        list.add(UPD_TIME);
        

        String intStr = "1000";// 需要特殊显示的标记;在原来基础上加1000 
        String tableName = "  WL_LINE A WHERE 1=1 ";  
 	    
        String orderStr = " ORDER BY A.LINE_NAME ASC "; 
       
        HashMap<String, String> hm = null; //DBHelper.formQueryToken(request);
        HashMap<String, String> hmSearch = new HashMap<String, String>();
        String searchFlag = BaseUtil.getStr(request.getParameter("searchFlag"));
        String s_role_name = BaseUtil.getStr(request.getParameter("s_role_name"));
        String s_role_desc = BaseUtil.getStr(request.getParameter("s_role_desc"));  
        
    	 if(!s_role_name.equals("")){
           	tableName += " AND A.ROLE_NAME LIKE '%"+s_role_name+"%' "; 
           	hmSearch.put("s_role_name", s_role_name);
         }
    	 if(!s_role_desc.equals("")){
           	tableName += " AND A.ROLE_DESC LIKE '%"+s_role_desc+"%'";
           	hmSearch.put("s_role_desc", s_role_desc);
         } 
    	 
        String pageNum = BaseUtil.getStr(request.getParameter("pageNums"));
        if(pageNum==null||pageNum.equals(""))
            pageNum = "1"; 
        List<?> resultL = DbUtil.queryStrForPage(list, tableName, intStr, hm, orderStr, Integer.parseInt(pageNum));
        int queryCount = DbUtil.queryStrCount(list, tableName, intStr, hm, orderStr);
        PageVO vo = DbUtil.pageObjFor(resultL, pageNum, queryCount);  
        
        request.setAttribute("hmSearch", hmSearch);
        request.setAttribute("searchFlag", searchFlag); 
         
        request.setAttribute("voResult", vo);
        request.setAttribute("qList", list);// 显示名称 
        return "lineList";
	}
    
    public String saveLine(){

    	Line line_e	= LineObj.getLineById(line.getLineCode());
    	if(line_e.getLineCode()==null||line.getLineCode().equals("")){
    		String sql	= "INSERT INTO WL_LINE(LINE_ID,LINE_CODE,LINE_NAME,LINE_LENGTH," +
    				"ACTIVE_FLAG,ADD_DATE,ADD_TIME,ADD_BY) " +
    				" VALUES(?,?,?,?,?,DATE_FORMAT(NOW(),'%Y%m%d') ,DATE_FORMAT(NOW(),'%H%i%s'),?)";
    		List<String> lt = new ArrayList<String>();
    		lt.add(line.getLineCode());
    		lt.add(line.getLineCode());
    		lt.add(line.getLineName());
    		lt.add(line.getLineLength().toString());
    		lt.add(line.getActiveFlag());
    		lt.add(((User)session.get("user")).getUserName());
    		boolean result	= DbUtil.excuSQL(lt, sql);
    		if(result){
    			request.setAttribute("showMsg","新增成功！");
    		}else{
    			request.setAttribute("showMsg","新增失败！");
    		} 		
    	}else{
    		request.setAttribute("showMsg","该编号已存在！");
    	}
    	
    	return lineList();
    }
    
    public String updateLine(){
		String sql	= "UPDATE WL_LINE SET LINE_NAME=?,LINE_LENGTH=?," +
				"ACTIVE_FLAG=?,UPD_DATE=DATE_FORMAT(NOW(),'%Y%m%d')," +
				"UPD_TIME=DATE_FORMAT(NOW(),'%H%i%s'),UPD_BY=? WHERE LINE_CODE=? ";
		List<String> lt = new ArrayList<String>();
		
		lt.add(line.getLineName());
		lt.add(line.getLineLength().toString());
		lt.add(line.getActiveFlag());
		lt.add(((User)session.get("user")).getUserName());
		lt.add(line.getLineCode());
		boolean result	= DbUtil.excuSQL(lt, sql);
		if(result){
			request.setAttribute("showMsg","更新成功！");
		}else{
			request.setAttribute("showMsg","更新失败！");
		} 		

    	return lineList();
    }
    
    public String getLineInfo(){
    	String id	= BaseUtil.getStr(request.getParameter("id"));
    	Line line = LineObj.getLineById(id);

    	JSONObject json = JSONObject.fromObject(line);
    	try {
			response.setCharacterEncoding("utf8");
			response.getWriter().print(json.toString());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return null;
    }
    
	 public String delLine(){
		 String id	= request.getParameter("id");

		 String sql	= "DELETE WL_LINE WHERE LINE_ID ='"+id+"'";

		 boolean result	= DbUtil.execute(sql);
		 if(result){
			 request.setAttribute("showMsg","删除成功！");
		 }else{
			 request.setAttribute("showMsg", "删除失败！");
		 }
		 return lineList();
	 }
}
